<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:dxl="http://www.lotus.com/dxl"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:session="java:ro.egmar.xslt.extensions.SessionExtension"
    exclude-result-prefixes="session xs dxl">
    
    <!-- Global parameters -->
    <xsl:param name="notesDbPath2"/> 
    <xsl:param name="mode2"/>
    <xsl:param name="unid2"/>
    
    <!-- Template for horizontal rule element -->
    <xsl:template match="dxl:horizrule">
        <hr style="widht:{@width};height:{@height};color:{@color};"/>
    </xsl:template>
    
    <!-- Template for computed text -->
    <xsl:template match="dxl:computedtext/dxl:code[@event='value']/dxl:formula">
        <xsl:value-of use-when="function-available('session:evaluate')" select="session:evaluate(string($notesDbPath2),string($unid2),string(text()))"/>
    </xsl:template>
    
    <!-- Template for simple table -->
    <xsl:template match="dxl:table">
        <xsl:param name="name"/>
        <xsl:variable name="style">
            <xsl:choose>
                <xsl:when test="dxl:border/@style='none'">
                    <xsl:value-of select="dxl:border/@style"/>
                </xsl:when>
                <xsl:when test="dxl:border/@style='solid'">
                    <xsl:value-of select="dxl:border/@style"/>
                </xsl:when>
                <xsl:when test="dxl:border/@style='double'">
                    <xsl:value-of select="dxl:border/@style"/>
                </xsl:when>
                <xsl:when test="dxl:border/@style='dot'">
                    <xsl:value-of select="concat(dxl:border/@style,'ted')"/>
                </xsl:when>
                <xsl:when test="dxl:border/@style='dash'">
                    <xsl:value-of select="concat(dxl:border/@style,'ed')"/>
                </xsl:when>
                <xsl:when test="dxl:border/@style='groove'">
                    <xsl:value-of select="dxl:border/@style"/>
                </xsl:when>
                <xsl:when test="dxl:border/@style='ridge'">
                    <xsl:value-of select="dxl:border/@style"/>
                </xsl:when>
                <xsl:when test="dxl:border/@style='inset'">
                    <xsl:value-of select="dxl:border/@style"/>
                </xsl:when>
                <xsl:when test="dxl:border/@style='outset'">
                    <xsl:value-of select="dxl:border/@style"/>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <table align="{if (contains(@widthtype,'fixed')) then substring-after(@widthtype,'fixed') else 'inherit'}" 
            style="border:{dxl:border/@width} {$style} {dxl:border/@color};width:{if (contains(@widthtype,'fixed')) then @refwidth else '100%'};">
            <tr>
                <xsl:for-each select="dxl:tablecolumn">
                    <td width="{if (contains(@width,'%')) then '' else @width}">
                    </td>
                </xsl:for-each>
            </tr>
            <xsl:apply-templates>
                <xsl:with-param name="name" select="translate($name,' ','')" />
            </xsl:apply-templates>
        </table>
    </xsl:template>
    
    <!-- Template for simple table row -->
    <xsl:template match="dxl:table/dxl:tablerow">
        <xsl:param name="name"/>
        <tr>
            <xsl:apply-templates>
                <xsl:with-param name="name" select="translate($name,' ','')" />
            </xsl:apply-templates>
        </tr>
    </xsl:template>
    
    <xsl:template match="dxl:table/dxl:tablerow/dxl:tablecell">
        <xsl:param name="name"/>
        <td rowspan="{@rowspan}" 
            colspan="{@columnspan}" 
            style="border:{@borderwidth} {../../@cellborderstyle} {../../@cellbordercolor};background:{@bgcolor} {if (dxl:cellbackground) then concat('url(',codepoints-to-string(39),'/',$notesDbPath2,'/',substring-before(dxl:cellbackground/dxl:imageref/@name,'|'),codepoints-to-string(39),')') else ''};">
            <xsl:apply-templates>
                <xsl:with-param name="name" select="translate($name,' ','')" />
            </xsl:apply-templates>
        </td>
    </xsl:template>
    
    <!-- Template for par element -->
    <xsl:template match="dxl:par[@def]">
        <xsl:param name="name"/>
        <p class="pardef-in-{$name}{@def}" style="margin:0;padding:0;margin-bottom:2px;">
            <xsl:apply-templates>
                <xsl:with-param name="name" select="translate($name,' ','')" />
            </xsl:apply-templates>
        </p>    
    </xsl:template>

	<!-- Template for par/text() element -->
    <xsl:template match="dxl:par[@def]/text()">
    	<xsl:value-of select="dxl:run/text()"/> 
    </xsl:template>
    
    <!-- Template for run element -->
    <xsl:template match="dxl:run">
        <xsl:param name="name"/>
        <span style="font-size:{dxl:font/@size};font-name:{dxl:font/@name};color:{dxl:font/@color};">
            <xsl:value-of select="text()"/>
            <xsl:apply-templates>
                <xsl:with-param name="name" select="translate($name,' ','')" />
            </xsl:apply-templates>
        </span>
    </xsl:template>
    
    <!-- Template for pardef element -->
    <xsl:template match="dxl:pardef[@id]">
        <xsl:param name="name"/>
        <xsl:variable name="fmlResult">
            <xsl:choose>
                <xsl:when test="dxl:code[event='hidewhen']/dxl:formula">
                    <xsl:value-of use-when="function-available('session:evaluate')" select="session:evaluate(string($notesDbPath2),string($unid2),string(text()))"/>    
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="''"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="hidden">
            <xsl:choose>
                <xsl:when test="@hide and contains(@hide,'notes')">
                    <xsl:value-of select="'true'"/>
                </xsl:when>
                <xsl:when test="@hide and $mode2='' and contains(@hide,'edit')">
                    <xsl:value-of select="'true'"/>
                </xsl:when>
                <xsl:when test="@hide and $mode2='read' and contains(@hide,'read')">
                    <xsl:value-of select="'true'"/>
                </xsl:when>
                <xsl:when test="@hide and $mode2='edit' and contains(@hide,'edit')">
                    <xsl:value-of select="'true'"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'false'"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <style type="text/css">
            <xsl:text>.pardef-in-</xsl:text><xsl:value-of select="$name"/><xsl:value-of select="@id"/><xsl:text>{</xsl:text>
            <xsl:text>display:</xsl:text>
            <xsl:choose>
                <xsl:when test="$hidden='true'">
                    <xsl:value-of select="'none'"/>
                </xsl:when>
                <xsl:when test="$fmlResult='' and $hidden='true'">
                    <xsl:value-of select="'none'"/>
                </xsl:when>
                <xsl:when test="$fmlResult='1,0'">
                    <xsl:value-of select="'none'"/>
                </xsl:when>
                <xsl:when test="$fmlResult='1,0' and $hidden='true'">
                    <xsl:value-of select="'none'"/>
                </xsl:when>
                <xsl:when test="$fmlResult='' and $hidden='false'">
                    <xsl:value-of select="'block'"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'block'"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:text>;</xsl:text>
            <xsl:text>text-align:</xsl:text>
            <xsl:value-of select="@align"/>
            <xsl:text>;</xsl:text>
            <xsl:text>}</xsl:text>
        </style>
    </xsl:template>
    
    <!-- Template for picture element-->
    <xsl:template match="dxl:picture/dxl:imageref">
        <img src="{concat('/',$notesDbPath2,'/',@name)}"/>
    </xsl:template>
    
    <!-- Template for action hotspot -->
    <xsl:template match="dxl:actionhotspot">
        <a href="">
            <xsl:value-of select="dxl:run/text()"/>
        </a>
    </xsl:template>
    
</xsl:stylesheet>